/*
 * @(#)LogoutAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.framework.web;

import java.sql.Connection;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationContainer;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.bean.AuditLoginOut;
import com.dcivision.framework.dao.AuditLoginOutDAObject;
import com.dcivision.user.auth.AuthenticationHandler;

/**
  LogoutAction.java

  This class is an action for logout.

    @author          Scott Tong
    @company         DCIVision Limited
    @creation date   18/07/2003
    @version         $Revision: 1.11.2.1 $
*/

public class LogoutAction extends AbstractAction {

  public static final String REVISION = "$Revision: 1.11.2.1 $";

  protected AuthenticationHandler authHandler = null;

  /** Creates a new instance of MaintUserGroupAction */
  public LogoutAction() {
    super();
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    try {
      this.authHandler = (AuthenticationHandler)Class.forName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.AUTHENICATION_CLASS)).newInstance();
    } catch (Exception e) {
      log.error("Authenication Handler class not found.", e);
      throw new ApplicationException(ErrorConstant.LOGIN_AUTH_CLASS_NOT_FOUND);
    }
    Connection conn = null;

    try {
      conn = getConnection(request);
      String userIP = request.getRemoteAddr();
      SessionContainer sessionCtn = this.getSessionContainer(request);
      sessionCtn.setLoginStatus(AuditLoginOut.STATUS_LOGOUT_SUCCESS);
      this.authHandler.Logout(sessionCtn, request);
      //Bill add for EIP-1909
      AuditLoginOut audit = auditLogout(conn,sessionCtn, userIP, sessionCtn.getLoginStatus(),request);
      request.getSession().removeAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
      request.getSession().invalidate();
      this.commit(request);
log.debug("Logout time="+audit.getLogoutDatetime());
      request.setAttribute("logout", "true");
      return mapping.findForward("LOGIN");
    } catch (Exception e) {
      log.error("Logout error", e);
      this.rollback(request);
      request.setAttribute("logout", "true");
      return mapping.findForward("LOGIN");
    }
  }

  protected AuditLoginOut auditLogout(Connection conn, SessionContainer sessionCtn, String userIP, String status, HttpServletRequest request) throws ApplicationException{
    AuditLoginOutDAObject auditLoginOutDAObject = new AuditLoginOutDAObject(sessionCtn, conn);
    AuditLoginOut auditLoginOut = new AuditLoginOut();
    auditLoginOut.setUserRecordID(sessionCtn.getUserRecordID());
    auditLoginOut.setIPAddress(userIP);
    auditLoginOut.setLoginDatetime(sessionCtn.getLoginTime());
    auditLoginOut.setLogoutDatetime(new Timestamp(System.currentTimeMillis()));
    auditLoginOut.setSessionID(request.getSession().getId());
    log.info(auditLoginOut);
    ((ApplicationContainer)request.getSession().getServletContext().getAttribute(
    GlobalConstant.APPLICATION_CONTAINER_KEY)).logoutUser(sessionCtn.getUserRecordID());
    auditLoginOutDAObject.updateBySessionID(auditLoginOut);
    auditLoginOut.setLoginStatus(status);
    return (AuditLoginOut)auditLoginOutDAObject.insertObject(auditLoginOut);
  }

  public String getMajorDAOClassName() {
    return(null);
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(null);
  }
}
